package com.wuhan.template.module.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseApplication;
import com.wuhan.template.base.BaseViewHolder;
import com.wuhan.template.base.MyBaseAdapter;
import com.wuhan.template.module.find.webview.WebViewActivity;
import com.wuhan.template.module.home.entity.HomeNewsEntity;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/15 0015.
 */
public class NewsBaseAdapter extends MyBaseAdapter<HomeNewsEntity>{

    private Context context;
    private ImageLoader loader;
    private DisplayImageOptions options;
    public NewsBaseAdapter(Context context, List<HomeNewsEntity> list) {
        super(context, list);
        this.context = context;
        loader = ((BaseApplication)(context.getApplicationContext())).getImageLoader();
        options = new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .build();
    }

    @Override
    public int getLayoutId() {
        return R.layout.home_activity_item;
    }

    @Override
    public BaseViewHolder getViewHolder() {

        return new ViewHolder();
    }

    @Override
    public void bindViews(View view, BaseViewHolder viewHolder) {
        ViewHolder holder = (ViewHolder) (viewHolder);
        final HomeNewsEntity item = getItem(holder.position);
        holder.title.setText(item.getTitle());
        String imageUrl = item.getIndexpic().getHost()+item.getIndexpic().getDir()+item.getIndexpic().getFilepath()+item.getIndexpic().getFilename();
        loader.displayImage(imageUrl,holder.image,options);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("url",item.getContent_url());
                intent.putExtra("title",item.getColumn_name());
                context.startActivity(intent);
            }
        });
    }

    public static class ViewHolder extends BaseViewHolder {

        @BindView(R.id.home_itemTitle)
        TextView title;
        @BindView(R.id.home_itemImage)
        ImageView image;
    }
}
